package face.pay.api.payter.channel;

import com.alibaba.fastjson.JSONObject;
import face.pay.constant.PayClient;
import face.pay.dao.TerminalInfoDao;
import face.pay.entity.TerminalInfo;
import face.pay.util.ResultMessage;
import face.pay.util.StringValidateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
@Slf4j
public abstract class AbstractChannel {

    public abstract String getChannelCode();

    public abstract ResultMessage facePay(JSONObject json);

    public abstract ResultMessage qrPay(JSONObject json);


    public abstract ResultMessage jsPay(JSONObject json);


    @Autowired
    TerminalInfoDao terminalInfoDao;

    /**
     * 条码类型
     * 支付宝
     * 微信
     * 云闪付
     * 其他
     * @param authCode
     * @return
     */
    public String barType(String authCode){

        String wxRex="^1[012345]\\d{16}$";
        String aliRex="^((2[5-9])|30)\\d{14,22}$";

        if(StringValidateUtils.isOk(wxRex,authCode)){
            return  PayClient.WECHAT;
        }else if(StringValidateUtils.isOk(aliRex,authCode)){
            return  PayClient.ALIPAY;
        }
        return  "OTHER";
    }

    public TerminalInfo terminalInfoValidate(String deviceId, String imei){
        log.info("设备验证");
        TerminalInfo terminalInfo = this.terminalInfoDao.getByDeviceId(deviceId);
        if(terminalInfo!=null){
            log.info("设备信息:"+terminalInfo.toString());
            //if(imei.equals(terminalInfo.getImei())){
                log.info("imei信息验证通过");
                if(terminalInfo.getStoreId()!=null){
                    log.info("设备已经绑定门店");
                    return  terminalInfo;
                }
            //}
        }
        return  null;
    }


    public  TerminalInfo getTermial( String deviceId){
        TerminalInfo terminalInfo = this.terminalInfoDao.getByDeviceId(deviceId);
        return  terminalInfo;

    }

}
